<?php require_once VIEWPATH . 'common/header.php'; ?>
<link rel="stylesheet" href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
<body>
<div class="page-container">
    <form class="form form-horizontal">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>合同号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<?=$contract_number ?? '' ?>" placeholder="" id="contract_number" name="">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>订单来源：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width:25%">
                <select id="origin" class="select">
                    <option <?php if(($origin ?? 1) == 1):?>selected<?php endif;?> value="1">淘宝</option>
                    <option <?php if(($origin ?? 0) == 2):?>selected<?php endif;?>  value="2">微店</option>
                    <option <?php if(($origin ?? 0) == 3):?>selected<?php endif;?>  value="3">微信</option>
                </select>
                </span>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>订单号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<?=$order_number ?? '' ?>" placeholder="" id="order_number" name="">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>签订时间：</label>

            <div class="formControls col-xs-8 col-sm-9">
                <input type="text"  value="<?= $signed_time ?? date('Y-m-d H:i:s', time()) ?>" onfocus="WdatePicker({ dateFmt:'yyyy-MM-dd HH:mm:ss',maxDate:'#F{$dp.$D(\'signed_time\')||\'%y-%M-%d\'}' })"
                       id="signed_time" class="input-text Wdate" style="width:180px;">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>名称：</label>

            <div class="formControls col-xs-8 col-sm-9">
                <input type="hidden" value="" name="id" class="col-xs-12 col-sm-9" />
                <input type="hidden" value="" id="corpId" name="corpId" class="col-xs-12 col-sm-9" />
                <input type="text" class="input-text" value="<?=$name ?? '' ?>" placeholder="" onClick="autosearchCargono()" class="col-xs-12 col-sm-9"  id="name" name="name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>数量：</label>

            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width:25%">
                <select id="number" class="select">
                    <?php for($i=1;$i<=10;$i++):?>
                    <option <?php if(($number ?? '') == $i):?>selected<?php endif;?> value="<?=$i?>"><?=$i?></option>
                    <?php endfor;?>
                </select>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>销售单价：</label>

            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<?=$unit_selling_price ?? '' ?>" placeholder="" id="unit_selling_price" name=""
                       style="width:90%">&nbsp;&nbsp;元
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>采购单价：</label>

            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" name="" id="purchase_price" placeholder="" value="<?=$purchase_price ?? '' ?>" class="input-text"
                       style="width:90%">&nbsp;&nbsp;元
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>供应商：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<?=$provider ?? '' ?>" placeholder="" id="provider" name="">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>联系：</label>

            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="<?=$contact ?? '' ?>" placeholder="" id="contact" name="">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>开票情况：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <span class="select-box" style="width:25%">
                <select id="invoice" class="select">
                    <option <?php if($invoice ?? 1 == 1):?>selected<?php endif;?> value="1">是</option>
                    <option <?php if($invoice ?? 0 == 2):?>selected<?php endif;?>  value="2">否</option>
                </select>
                </span>
            </div>
        </div>


        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-2">备注：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <textarea id="remark" name="" cols="" rows="" class="textarea" placeholder="说点什么...最少输入10个字符" datatype="*10-100" dragonfly="true"> <?=$remark ?? '' ?></textarea>
            </div>
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <span onClick="save();" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe632;</i> 保存
                </span>
                <span onClick="layer_close();" class="btn btn-default radius">&nbsp;&nbsp;取消&nbsp;&nbsp;</span>
            </div>
        </div>
        <input type="hidden" id="bill_id" value="<?=$bill_id??''?>">
    </form>
</div>
<?php require_once VIEWPATH . 'common/footer.php'; ?>
</body>
</html>

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="<?= STATICURL; ?>lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script type="text/javascript">

    mustNum($("#number"));
    mustNum($("#purchase_price"));
    mustNum($("#unit_selling_price"));

    function autosearchCargono(){
        $("#name").autocomplete({
            minLength: 1,
            source: function(request, response) {
                var term = request.term;
                $.ajax({
                    url: "/bill/searchName",
                    dataType: "json",
                    type: "get",
                    data: {
                        billname: term
                    },
                    success: function(data) {
                        response($.map(data.data,
                            function(item) {
                                $("#corpId").val(item);
                                return {
                                    lable: item,
                                    value: item
                                }
                            }));
                    }
                });
            }
        });
    }
    function mustNum($object){
        $object.bind({
                keyup:function(){
                     var txt = $object.val();
                     var reg = /^[\d]+$/;
                     if(!reg.test(txt) ){
                            $(this).val('');
                     }
                },
                focus:function(){
                    if ($(this).val() == 0){
                        $(this).val('');
                    }
                },
                blur:function(){
                    if ($(this).val() == ''){
                        $(this).val('');
                    }
                }
        }); 
    }
    
    function save() {
        var bill_id = $("#bill_id").val();
        var contract_number = $("#contract_number").val();
        var order_number = $("#order_number").val();
        var signed_time = $("#signed_time").val();
        var name = $("#name").val();
        var number = $("#number").val();
        var unit_selling_price = $("#unit_selling_price").val();
        var purchase_price = $("#purchase_price").val();
        var provider = $("#provider").val();
        var contact = $("#contact").val();
        var invoice = $("#invoice").val();
        var origin = $("#origin").val();
        var remark = $("#remark").val();
        if (contract_number == '') {
            layer.msg("请输入合同号");
            return false;
        }
        var params = {
            contract_number: contract_number,
            order_number: order_number,
            signed_time: signed_time,
            name: name,
            number: number,
            unit_selling_price: unit_selling_price,
            purchase_price: purchase_price,
            provider: provider,
            contact: contact,
            invoice: invoice,
            origin: origin,
            remark:remark
        };
        if(bill_id){
            params.bill_id = bill_id;
        }
        $.post("/bill/update", params, function (data) {
            if (data.code != 1) {
                layer.msg(data.message);
                return false;
            }
            layer.msg(data.message);
            parent.location.reload();
        },'json');
    }
</script>

